package jinyilw.swing.visuals.comp;

import jinyilw.common.Operator;
import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.RenderingConfig;

import javax.swing.Icon;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class RoundIconButton extends MouseActComp
{
	private static final long serialVersionUID = 4394646231131149226L;
	protected static final AlphaComposite alphaComposite = ComponentTools.alphaComposite;
	protected static final Color lightColor = new Color(220, 220, 220);
	protected static Color darkBorderColor = new Color(0, 0, 0, 130);
	protected static Color lightBorderColor = new Color(150, 150, 150, 100);
	protected static float defaultAlpha = 0.3f, hoverAlpha = 0.45f;
	//	protected Color graColor1 = lightColor;
	//	protected Color graColor2 = Color.white;
	private Icon icon, overIcon;

	public RoundIconButton()
	{
		enableMouseAct();
		setBackground(lightColor);
	}

	public RoundIconButton(Icon icon)
	{
		this();
		setIcon(icon);
	}

	public void setIcon(Icon icon)
	{
		setActionIcon(icon, icon);
	}

	public void setActionIcon(Icon icon, Icon overIcon)
	{
		this.icon = icon;
		this.overIcon = overIcon;
		if (icon != null)
		{
			int w = icon.getIconWidth(), h = icon.getIconHeight();
			if (w != getWidth() || h != getHeight())
			{
				createPaint(w, h);
				setSize(w, h);
				return;
			}
		}
		if (isShowing())
			repaint();
	}

	public void addActionListener(final ActionListener listener)
	{
		addClickOperator(o -> listener.actionPerformed(null));
	}

	public void addDoubleClickOperator(final Operator<MouseEvent> operator)
	{
		addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent e)
			{
				if (isEnabled() && e.getClickCount() == 2)
					operator.operate(e);
			}
		});
	}

	private GradientPaint borderPaint, pressBorderPaint;
	private void createPaint(int w, int h)
	{
		borderPaint = new GradientPaint(1, 1, lightBorderColor, 1, h - 1,
				darkBorderColor);
		pressBorderPaint = new GradientPaint(1, 1, lightBorderColor, 1, h - 1,
				lightBorderColor);
	}

	private final RenderingConfig renderingConfig = new RenderingConfig();
	@Override
	public void paint(Graphics g)
	{
		Icon ico = isRollover ? overIcon : icon;
		int h = getHeight(), w = getWidth();
		boolean isEnabled = isEnabled();
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = g2d.getComposite();
		renderingConfig.setAAPainting(g2d);
		GradientPaint borderP;
		if (isEnabled && isPressed)
		{
			g2d.translate(0.5, 0.5);
			borderP = pressBorderPaint;
		} else
			borderP = borderPaint;
		float alpha = !isEnabled ? 0.1f : isRollover ? hoverAlpha : defaultAlpha;
		g2d.setComposite(alphaComposite.derive(alpha));
		g2d.setColor(getBackground());
		g2d.fillOval(1, 1, w - 2, h - 2);

		g2d.setComposite(alphaComposite.derive(isEnabled ? 0.85f : 0.3f));

		//		if (isEnabled && isRollover)
		//			g2d.setColor(RThemeColors.themeColor);
		//		else
		g2d.setPaint(borderP);
		g2d.drawOval(1, 1, w - 2, h - 2);

		g2d.setComposite(alphaComposite.derive(isEnabled ? 1f : 0.25f));
		if (ico != null)
			ico.paintIcon(this, g2d, 0, 0);
		if (isEnabled && isPressed)
			g2d.translate(-0.5, -0.5);
		renderingConfig.resetAAPainting(g2d);
		g2d.setComposite(composite);
	}
}
